package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.IRechargeRuleService;
import com.younion.vo.RechargeRuleVO;

/**
 * @author wells
*/
public class RechargeRuleAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(RechargeRuleAction.class);

	private IRechargeRuleService rechargeRuleService = null;

	private RechargeRuleVO rechargeRuleVO = new RechargeRuleVO();

	private List<RechargeRuleVO> rechargeRuleList;// 结果集列表
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	public String listRechargeRule() {
		try {
			Map paramMap = null;
			String cardEntityId = request.getParameter("cardEntityId");
			System.out.println("cardEntityId:"+cardEntityId);
			if (rechargeRuleVO != null) {
				rechargeRuleVO.setCardEntityId(new Integer(cardEntityId));
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(rechargeRuleVO));
			}
			// 获取总记录数
			int total = rechargeRuleService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			rechargeRuleList = rechargeRuleService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	public String addRechargeRule() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	public String editRechargeRule() {
		try {
			rechargeRuleVO = rechargeRuleService.getById(rechargeRuleVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	public String saveRechargeRule() {
		try {
			rechargeRuleVO = rechargeRuleService.save(rechargeRuleVO);
			this.addLog(3, "更新充值规则："+rechargeRuleVO.getId(), "更新充值规则",getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	public String deleteRechargeRule() {
		try {
			String pkId = request.getParameter("key");
			rechargeRuleService.remove(Arrays.asList(pkId.split(",")));
			this.addLog(4, "删除充值规则："+pkId, "删除充值规则",getSessionUser());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	public String viewRechargeRule() {
		try {
			String pkId = request.getParameter("key"); 
			rechargeRuleVO = rechargeRuleService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public RechargeRuleVO getRechargeRuleVO() {
		return rechargeRuleVO;
	}

	public void setRechargeRuleVO(RechargeRuleVO rechargeRuleVO) {
		this.rechargeRuleVO = rechargeRuleVO;
	}

	public void setRechargeRuleService(IRechargeRuleService rechargeRuleService) {
		this.rechargeRuleService = rechargeRuleService;
	}

	public List<RechargeRuleVO> getRechargeRuleList() {
		return rechargeRuleList;
	}

	public void setRechargeRuleList(List<RechargeRuleVO> rechargeRuleList) {
		this.rechargeRuleList = rechargeRuleList;
	}

}